








module vga_ctrl(clock, x, y, VGA_HS, VGA_VS);
	input wire		clock;
	output wire [11:0]	x;
	output wire [11:0]	y;
	output reg		VGA_HS;	
	output reg		VGA_VS;	
	
/*	http://www.tinyvga.com/vga-timing/800x600@60Hz	*/
//Horizontal timing
	parameter h_fp = 40;
	parameter h_sp = 128;
	parameter h_bp = 88;
	parameter h_va = 800;
	parameter h_all = h_fp + h_sp + h_bp + h_va;
//Vertical timing
	parameter v_fp = 1;
	parameter v_sp = 4;
	parameter v_bp = 23;
	parameter v_va = 600;
	parameter v_all = v_fp + v_sp + v_bp + v_va;
	
	reg [11:0] pixel_count;
	reg [11:0] row_count;
	
	assign x = pixel_count - (h_fp + h_sp + h_bp);
	assign y = row_count - (v_fp + v_sp + v_bp);
	
	always @(posedge clock)begin
		if(pixel_count == h_all-1)
			pixel_count <= 0;
		else
			pixel_count <= pixel_count + 1;
		
		if(pixel_count == h_fp-1)
			VGA_HS <= 0;
		if(pixel_count == (h_fp + h_sp)-1)
			VGA_HS <= 1;
	end
	
	always @(negedge VGA_HS)begin
		if(row_count == v_all-1)
			row_count <= 0;
		else
			row_count <= row_count + 1;
		
		if(row_count == v_fp-1)
			VGA_VS <= 0;
		if(row_count == (v_fp + v_sp)-1)
			VGA_VS <= 1;
	end
endmodule


module vga(clock, VGA_HS, VGA_VS, VGA_DATA, address_a, clock_a, q_a, using_buffer);
	input wire clock;
	output wire VGA_HS;
	output wire VGA_VS;
	output reg [11:0] VGA_DATA;
	output wire [15:0] address_a;
	output wire clock_a;
	input wire [3:0] q_a;
	output reg using_buffer;
	
	wire [11:0] x;
	wire [11:0] y;
	
	assign clock_a = ~clock;
	assign address_a = y/4*200+x/4;
	
	always @* begin
		if(y[11])
			using_buffer <= 0;
		else
			using_buffer <= 1;
		
		if(x[11] | y[11])
			VGA_DATA <= 0;
		else
			VGA_DATA <= {q_a, q_a, q_a}; //Change this line to adjust hue
	end
 
	vga_ctrl(clock, x, y, VGA_HS, VGA_VS);
endmodule













